// pages/yuedu/yuedu.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    items:"",
    id:0,
    laststate: "",
    answer1:["cuo","cuo","cuo","cuo"],
    answer2: ["cuo", "cuo", "cuo", "cuo"],
    answer3: ["cuo", "cuo", "cuo", "cuo"],
    answer4: ["cuo", "cuo", "cuo", "cuo"],
    state1: ["none", "none", "none", "none"],
    state2: ["none", "none", "none", "none"],
    state3: ["none", "none", "none", "none"],
    state4: ["none", "none", "none", "none"],
    ckjx: ["none", "none", "none", "none"],
    analyze: ["none", "none", "none", "none"],
    touch:[0,0,0,0]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let api = this.apichange(options.id);
    console.log(options.id)
    wx.request({
      url: `${app.globalData.apiurl}${api}/list`,
      method:"GET",
      data:{
        account:10
      },
      success:res=>{
        console.log(res.data)
        let count = res.data.length
        for (let i = 0; i < count; i++) {
          let ans=res.data[i].answer.split(",")
          console.log(ans)
          let answers = [];
          for(let j=0;j<4;j++){
            if (ans[j] == "A") {
              answers[j] = ["dui", "cuo", "cuo", "cuo"]
            }
            if (ans[j] == "B") {
              answers[j] = ["cuo", "dui", "cuo", "cuo"]
            }
            if (ans[j] == "C") {
              answers[j] = ["cuo", "cuo", "dui", "cuo"]
            }
            if (ans[j] == "D") {
              answers[j] = ["cuo", "cuo", "cuo", "dui"]
            }
          }
         console.log(answers)
         res.data[i].answer=answers
         console.log(res.data[i].answer)
        }
        console.log(res.data)
        this.setData({
          items:res.data,
          answer: res.data[this.data.id].answer
        })
      }
    })
    console.log(this.data.answer1)
  },
  //下一题点击事件
  bindNext: function (e) {
    let state = this.data.state;
    state = ["none", "none", "none", "none"];
    let touch = [0,0,0,0]
    this.setData({
      state1: state,
      state2: state,
      state3: state,
      state4: state,
      ckjx: state,
      analyze: state,
      laststate: "",
      touch:touch
    })
    if (this.data.id == 9) {
      wx.showToast({
        title: '已经做完10题！点左上角返回！',
        icon: "none"
      })
      return false;
    } else {
      let ide = this.data.id * 1 + 1
      console.log(ide)
      this.setData({
        id: ide,
        answer: this.data.items[ide].answer
      })
      console.log(this.data.items[this.data.id])
    }
  },
  //上一题点击事件
  bindLast: function (e) {
    let state = this.data.state;
    state = ["none", "none", "none", "none"];
    let touch=[0,0,0,0]
    this.setData({
      state1: state,
      state2: state,
      state3: state,
      state4: state,
      ckjx: state,
      analyze: state,
      state: state,
      touch: touch
    })
    if (this.data.id == 0) {
      wx.showToast({
        title: '无上一题！',
        icon: "none"
      })
      return false;
    } else {
      let ide = this.data.id * 1 - 1
      this.setData({
        id: ide,
        answer: this.data.items[ide].answer,
        touch: 0
      })
      if (ide == 0) {
        this.setData({
          laststate: ""
        })
      }
      console.log(this.data.items[this.data.id])
    }
  },
  //选项点击事件
  chance: function (e) {
    console.log(e.currentTarget.id)
    if (e.currentTarget.id >= 0 && e.currentTarget.id<=3){
      if(this.data.touch[0]==1){
        return false;
      }
      if (this.data.answer[0][e.currentTarget.id] == "cuo") {
        if (this.data.state1[e.currentTarget.id] == "none") {
          let state = this.data.state1;
          state = ["", "", "", ""];
          let ckjx=this.data.ckjx;
          ckjx[0]="";
          this.setData({
            state1: state,
            ckjx:ckjx,
          })
        }
      } else {
        let state = this.data.state1;
        state[e.currentTarget.id] = "";
        let touch = this.data.touch;
        touch[0]=1;
        this.setData({
          state1: state,
          touch: touch
        })
      }
    }
    if (e.currentTarget.id >= 4 && e.currentTarget.id <= 7) {
      e.currentTarget.id = e.currentTarget.id-4
      if (this.data.touch[1] == 1) {
        return false;
      }
      if (this.data.answer[1][e.currentTarget.id] == "cuo") {
        if (this.data.state2[e.currentTarget.id] == "none") {
          let state = this.data.state2;
          state = ["", "", "", ""];
          let ckjx = this.data.ckjx;
          ckjx[1] = "";
          this.setData({
            state2: state,
            ckjx: ckjx,
          })
        }
      } else {
        let state = this.data.state2;
        state[e.currentTarget.id] = "";
        let touch = this.data.touch;
        touch[1] = 1;
        this.setData({
          state2: state,
          touch: touch
        })
      }
    }
    if (e.currentTarget.id >= 8 && e.currentTarget.id <= 11) {
      e.currentTarget.id = e.currentTarget.id - 8
      if (this.data.touch[2] == 1) {
        return false;
      }
      if (this.data.answer[2][e.currentTarget.id] == "cuo") {
        if (this.data.state3[e.currentTarget.id] == "none") {
          let state = this.data.state3;
          state = ["", "", "", ""];
          let ckjx = this.data.ckjx;
          ckjx[2] = "";
          this.setData({
            state3: state,
            ckjx: ckjx,
          })
        }
      } else {
        let state = this.data.state3;
        state[e.currentTarget.id] = "";
        let touch = this.data.touch;
        touch[2] = 1;
        this.setData({
          state3: state,
          touch: touch
        })
      }
    }
    if (e.currentTarget.id >= 12 && e.currentTarget.id <= 15) {
      e.currentTarget.id = e.currentTarget.id - 12
      if (this.data.touch[3] == 1) {
        return false;
      }
      if (this.data.answer[3][e.currentTarget.id] == "cuo") {
        if (this.data.state4[e.currentTarget.id] == "none") {
          let state = this.data.state4;
          state = ["", "", "", ""];
          let ckjx = this.data.ckjx;
          ckjx[3] = "";
          this.setData({
            state4: state,
            ckjx: ckjx,
          })
        }
      } else {
        let state = this.data.state4;
        state[e.currentTarget.id] = "";
        let touch = this.data.touch;
        touch[3] = 1;
        this.setData({
          state4: state,
          touch: touch
        })
      }
    }
  },
  //查看解析
  ckAnswer1:function(e){
    let ckjx=this.data.ckjx
    ckjx[0] = "none"
    let analyze = this.data.analyze
    analyze[0]=""
    this.setData({
      ckjx:ckjx,
      analyze:analyze
    })
  },
  ckAnswer2: function (e) {
    let ckjx = this.data.ckjx
    ckjx[1] = "none"
    let analyze = this.data.analyze
    analyze[1] = ""
    this.setData({
      ckjx: ckjx,
      analyze: analyze
    })
  },
  ckAnswer3: function (e) {
    let ckjx = this.data.ckjx
    ckjx[2] = "none"
    let analyze = this.data.analyze
    analyze[2] = ""
    this.setData({
      ckjx: ckjx,
      analyze: analyze
    })
  },
  ckAnswer4: function (e) {
    let ckjx = this.data.ckjx
    ckjx[3] = "none"
    let analyze = this.data.analyze
    analyze[3] = ""
    this.setData({
      ckjx: ckjx,
      analyze: analyze
    })
  },
  apichange: function (id) {
    if (id == 2) {
      let name = "gqbYwModern1"
      return name;
    }
    if (id == 3) {
      let name = "gqbYwModern2"
      return name;
    }
    if (id == 11){
      let name ="gqbYyModern"
      return name;
    }
    if(id == 21){
      let name = "gqzYyModern"
      return name;
    }
    if (id == 24) {
      let name = "gqzYwModern1"
      return name;
    }
    if (id == 39) {
      let name = "zsbYyModern"
      return name;
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})